package com.person.jc.tag;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.utils.Utils;

/*****************************************************************************
 * @程序名: FunMenuTag.java
 * @日期 : 2014-5-1
 * @作者 : 武智雄 (wuzhixiong@vvise.com)
 * @模块 : [项目]-[一级菜单]-[二级菜单]-[查询拼接权限菜单] 
 * @描述 : [1.查询角色权限使用roleId,companyId
 *         2.查询用户权限使用userId,companyId
 *          3.查询菜单版本定义只使用tmsVersion] 
 * ------------------------------------------------------------ 
 * 修改历史 
 * 序号             日期                      修改人                  修改原因 
 * 1
 * 2
 ******************************************************************************/
public class SysMenuTag extends BodyTagSupport {
    private static final long serialVersionUID = 1L;

    private String roleId;
    private String userId;
    private String disabled;
    private String titleName;

    public String getRoleId() {
        return roleId;
    }
    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getDisabled() {
        return disabled;
    }
    public void setDisabled(String disabled) {
        this.disabled = disabled;
    }
    
    public String getTitleName() {
        return titleName;
    }
    public void setTitleName(String titleName) {
        this.titleName = titleName;
    }
    
	public int doEndTag() throws JspException {
        JspWriter out = pageContext.getOut();
        try {
            out.write(spellHtml());
        }
        catch (Exception e) {
            throw new JspException("Error:" + e.getMessage());
        }
        return EVAL_BODY_INCLUDE;
    }
    
    /**
     * 
     * @Description: [拼接页面]
     * @Author: 武智雄 (wuzhixiong@vvise.com)
     * @Date: 2014-5-4
     * @@return
     */
    private String spellHtml(){
        List<Record> menuList = getMenuList();
        StringBuffer html = new StringBuffer("");
        if(menuList.size() > 0){
            html.append("<div style=\"position: fixed; z-index:1; top:0px; left:0px; width:100%;\" id=\"fixed_top\">");
            html.append("<div style=\"background-color: #fff; padding:8px 0px; padding-left:10px;\">");
            html.append("<span style=\"border:none;font-weight: bold;\">"+titleName+"</span>");
            html.append("</div>");
            html.append("<div style=\"width:100%;\">");
            html.append("<div style=\"float:left; background-color:#F0A628; padding-left:10px; color:#fff; border-right:1px solid #fff;  width:180px; height:30px; line-height:30px; font-weight:700; font-size:14px;\">系统菜单</div>");
            html.append("<div style=\"float:left; background-color:#A7A398; padding-left:10px;  color:#fff; width:660px; height:30px; line-height:30px; font-weight:700; font-size:14px;\">功能菜单</div>");
            html.append("</div>");
            html.append("</div>");
            html.append("<div id=\"fixed_border\" style=\"position: fixed; z-index:2; top:60px; left:190px; width:1px; background-color:#D1D1D1;\"></div>");
           
            html.append("<div style=\"margin-top:40px;\">");
            int t = 0;
            for(int i = 0;i < menuList.size();i++){
            	Record map = menuList.get(i);
                int marginLeft = 3;
                int mWidth = 80;
                int menuLvl = Integer.parseInt(String.valueOf(map.get("menu_lvl")));
                if(!"1".equals(String.valueOf(map.get("if_function_menu")))){
                    mWidth = (marginLeft + 6) * (3 - menuLvl) + 160;
                    if(menuLvl == 3){
                    	mWidth -= 4;
                    }
                    marginLeft = (marginLeft + 10) * menuLvl;
                    html.append("<br />");
                }
                
                if(menuLvl == 1){
                    if(i != 0)
                        html.append("</div>");
                    String divBgColor = (t % 2 == 0) ? "#f1f1f1" : "#fff";
                    t++;
                    html.append("<div style=\"width: 100%;background:"+ divBgColor +";padding: 5px 0px;margin:5px 0px;\">");
                }
                
                String flag = String.valueOf(map.get("flag"));
                String flag1 = String.valueOf(map.get("flag1"));
                html.append("<div style=\"width:"+ mWidth +"px;height:26px;line-height:26px;display:inline-block;margin-left: "+ marginLeft +"px;\">");
                html.append("<input type=\"checkbox\" id=\""+ map.get("menu_id") +"\" name=\""+ map.get("p_menu_id") +"\" value=\""+ map.get("menu_id") +"\"");
                html.append(" onclick=\"check(this)\" defMenu=\""+ map.get("if_def_menu") +"\" isFunMenu=\""+ map.get("if_function_menu") +"\"");
                
                if(Utils.isNotEmpty(flag1))
                    html.append(" disabled defDisabled");
                
                if(Utils.isNotEmpty(flag1) || Utils.isNotEmpty(flag))
                    html.append(" checked=\"true\"");
                
                html.append(disabled + "/>");
                html.append("<label for=\""+ map.get("menu_id") +"\" style=\"cursor: pointer;margin-left:4px;");
                
                if(Utils.isNotEmpty(flag1))
                    html.append("color:green;");
                
                html.append("\">"+ map.get("menu_name") +"</label>");
                html.append("<input type=\"hidden\" id=\"menu_"+ map.get("menu_id") +"\" name=\"p_menuId\" ");
                
                if(Utils.isNotEmpty(flag))
                    html.append(" value=\""+ map.get("menu_id") +"\"");
                html.append(" />");
                html.append("</div>");
                if(i == menuList.size())
                    html.append("</div>");
            }
            html.append("</div>");
            html.append("<script language=\"javaScript\">");
            html.append("$(function(){");
            if(Utils.isEmpty(disabled)){
                for(int i = 0;i < menuList.size();i++){
                	Record map = menuList.get(i);
                    
                    if(!"1".equals(map.get("menu_lvl")) && !"1".equals(map.get("if_function_menu"))){
                        html.append(" var menus = document.getElementsByName('"+map.get("menu_id")+"');");
                        html.append(" var defMenu;");
                        html.append(" var isDisabled = false;");
                        html.append(" for(var i = 0;i < menus.length;i++){");
                        html.append("   if('"+map.get("menu_id")+"' != menus[i].id && $(menus[i]).attr(\"isFunMenu\") == 1){");
                        html.append("   if($(menus[i]).attr(\"defMenu\") == 1){");
                        html.append("       defMenu = menus[i];");
                        html.append("   }else if(menus[i].checked){");
                        html.append("       isDisabled = true;");
                        html.append("   }}} ");
                        html.append(" if(isDisabled && defMenu){");
                        html.append("   defMenu.disabled = isDisabled;");
                        html.append("}");
                    }
                    //html.append("initMenu("+ map.get("MENU_LVL") +","+ map.get("IF_FUNCTION_MENU") +","+ map.get("MENU_ID") +");");
                }
            }
            html.append("$('.main_cont').css({'padding-bottom':'0px'});");
            html.append(" var window_height = $(window).height()-$('#fixed_top').outerHeight(true);");
            html.append("$('#fixed_border').css({'height':window_height+'px'});");
            html.append("});");
            html.append("</script>");
        }
        return html.toString();
    }
    
    /**
     * 
     * @Description: [查询菜单数据]
     * @Author: 武智雄 (wuzhixiong@vvise.com)
     * @Date: 2014-5-4
     * @@return
     */
    private List<Record> getMenuList(){
        String tableName = "ts_sys_menu";
        String roleTable = "ts_role_menu_vcl";
        
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT distinct t.*,ifnull((select menu_lvl from "+ tableName +" where menu_id = t.p_menu_id),0) p_menu_lvl ");
        
        if(Utils.isNotEmpty(userId))
            sb.append(",t2.menu_id flag1");
        
        sb.append(",t1.menu_id flag");
        
        sb.append(" from (");
        sb.append("select t.* FROM "+ tableName +" t");
        
        sb.append(" WHERE t.if_valid = 1");
        
        sb.append(") t");
        if(Utils.isNotEmpty(roleId))
            sb.append(" LEFT JOIN (SELECT t1.menu_id FROM "+ roleTable +" t1 WHERE t1.role_id = '"+ roleId +"') t1 ON t.menu_id = t1.menu_id");
        if(Utils.isNotEmpty(userId)){
            sb.append(" LEFT JOIN (SELECT t1.menu_id FROM ts_user_menu_vcl t1 WHERE t1.user_id = '"+ userId +"') t1 ON t.menu_id = t1.menu_id");
            sb.append(" LEFT JOIN (SELECT t2.menu_id FROM ts_user_role_vcl t3,"+ roleTable +" t2 WHERE t2.role_id = t3.role_id AND t3.user_id = '"+ userId +"') t2 ON t2.menu_id = t.menu_id");
        }
        sb.append(" ORDER BY t.menu_id");
        return Db.find(sb.toString());
    }
}
